package com.lorry;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class ThreadTest {

    //继承Thread类并重写run方法
    public static class MyThread extends Thread{

        int a;
        int b;

        public MyThread(int a, int b) {
            this.a = a;
            this.b = b;
        }
        //方便传参
        public MyThread(Runnable target, String name, int a, int b) {
            super(target, name);
            this.a = a;
            this.b = b;
        }

        @Override
        public void run() {
            System.out.println("I am a child thread");
        }
    }


    //可以实现多个接口
    public static class RunnableTask implements Runnable{
        int a = 1;

        @Override
        public void run() {
            System.out.println(a);
            System.out.println("I am a child thread");
        }
    }

    public static class CallerTask implements Callable<String>{

        @Override
        public String call() throws Exception {
            return "hello";
        }
    }

    public static void main(String[] args) {
        //创建线程
//        MyThread thread = new MyThread();
        RunnableTask task = new RunnableTask();
        //启动线程
        new Thread(task).start();
        new Thread(task).start();

//        thread.start();
        //创建异步任务
        FutureTask<String> futureTask = new FutureTask<>(new CallerTask());

        //启动线程
        new Thread(futureTask).start();
        try {
            //等待任务执行完毕，并返回结果
            String result = futureTask.get();
            System.out.println(result);
        } catch (ExecutionException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}




